package utils

import "github.com/shopspring/decimal"

/**
 * 小数点后保留位数，2位，四舍五入
 */
const DigitNumberAfterDot = 2

/**
 * 劳务报酬所得，每次收入不超过四千元的，减除费用八百元；四千元以上的，减除百分之二十的费用，其余
 * 额为应纳税所得额。然后适用比例税率，税率为百分之二十。
 *
 * |收入(x)         |应纳税部分(y)     |   阶梯         |税率        | 纳税额           | 速扣系数     |
 * -----------------------------------------------------------------------------------------------
 * |x<=800         |      0       |     0             |  0        | 0               | 0           |
 * |800<x<=4000    |    x-800     |    y<=20000       |  20%      | (x-800)*20%     | 0           |
 * |4000<x<=20000  |    0.8x      |    y<=20000       |  20%      | 0.8x*20%        | 0           |
 * |20000<x<=50000 |    0.8x      |    20000<y<=50000 |  30%      | 0.8x*30%        | 2000        |
 * |x>50000        |    0.8x      |    y>50000        |  40%      | 0.8x*40%        | 7000        |
 *
 * @param originIncome
 * @return
 */

func IncomeAfterTax(originIncome float64) float64 {
	var taxIncome float64 = 0
	var taxRate float64 = 0
	var quickDeducData float64 = 0
	if originIncome <= 800 {
		return originIncome
	} else if originIncome > 800 && originIncome <= 4000 {
		taxIncome = originIncome - 800
		taxRate = 0.2
	}
	if originIncome > 4000 {
		taxIncome = originIncome * 0.8
	}
	if taxIncome <= 20000 {
		taxRate = 0.2
	} else if taxIncome > 20000 && taxIncome <= 50000 {
		taxRate = 0.3
		quickDeducData = 2000
	}
	if taxIncome > 50000 {
		taxRate = 0.4
		quickDeducData = 7000
	}
	incomeAfterTax := originIncome - (taxIncome*taxRate - quickDeducData)
	return RoundHalfUpValue(incomeAfterTax, DigitNumberAfterDot)
}

// 保留两位小数，四舍五入
func RoundHalfUpValue(value float64, digitNumberAfterDot int32) float64 {
	sum := decimal.NewFromFloat(value).Round(digitNumberAfterDot)
	newSum, _ := sum.Float64()
	return newSum
}
